package com.example.a13001.shoppingmalltemplate.activitys;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a13001.shoppingmalltemplate.R;
import com.example.a13001.shoppingmalltemplate.adapters.OrderListLvAdapter;
import com.example.a13001.shoppingmalltemplate.base.AppConstants;
import com.example.a13001.shoppingmalltemplate.base.BaseActivity;
import com.example.a13001.shoppingmalltemplate.modle.CommonResult;
import com.example.a13001.shoppingmalltemplate.modle.Order;
import com.example.a13001.shoppingmalltemplate.mvpview.OrderListView;
import com.example.a13001.shoppingmalltemplate.presenter.OrderListPredenter;
import com.example.a13001.shoppingmalltemplate.utils.MyUtils;
import com.example.a13001.shoppingmalltemplate.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderListActivity extends BaseActivity implements OrderListLvAdapter.changeOrderStateInterface {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title_center)
    TextView tvTitleCenter;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_orderlist_all)
    TextView tvOrderlistAll;
    @BindView(R.id.tv_orderlist_nopay)
    TextView tvOrderlistNopay;
    @BindView(R.id.tv_orderlist_nogroup)
    TextView tvOrderlistNogroup;
    @BindView(R.id.tv_orderlist_delivergoods)
    TextView tvOrderlistDelivergoods;
    @BindView(R.id.tv_orderlist_takegoods)
    TextView tvOrderlistTakegoods;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.view5)
    View view5;
    @BindView(R.id.lv_orderlist)
    ListView lvOrderlist;
    @BindView(R.id.srfl_orderlist)
    SmartRefreshLayout srflOrderlist;
    private OrderListLvAdapter mAdapter;
    private List<Order.OrderListBean> mList;
    private String type;
    private OrderListPredenter orderListPredenter = new OrderListPredenter(OrderListActivity.this);
    private String code;
    private String timestamp;
    private int pageindex = 1;
    private static final String TAG = "OrderListActivity";
    private String orderStatus = "";//查询订单状态，1 待支付 2 未发货 3 已发货 4 已签收
    private int orderType;
    private String type1="1";//订单商品类型，1 普通商品 2 虚拟商品，为空则所有

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
        tvTitleCenter.setText("我的订单");

        initData();
    }

    OrderListView orderListView = new OrderListView() {
        //获取订单列表
        @Override
        public void onGetOrderList(Order order) {
            Log.e(TAG, "onGetOrderList: " + order.toString());
            int status = order.getStatus();
            if (status > 0) {
                mList.addAll(order.getOrderList());
                mAdapter.notifyDataSetChanged();

            } else {

                Toast.makeText(OrderListActivity.this, "" + order.getReturnMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        //取消未支付订单
        @Override
        public void onSuccessCancelOrder(CommonResult commonResult) {
            Log.e(TAG, "onSuccessCancelOrder: " + type + commonResult.toString());
            int status = commonResult.getStatus();
            if (status > 0) {
                switch (orderType) {
                    case 1:
                        tvOrderlistAll.performClick();
//                        mAdapter.notifyDataSetChanged();
                        break;
                    case 2:
                        tvOrderlistNopay.performClick();
//                        mAdapter.notifyDataSetChanged();
                }
            } else {
                Toast.makeText(OrderListActivity.this, "" + commonResult.getReturnMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        //确认收货
        @Override
        public void onSuccessAffirmOrder(CommonResult commonResult) {
            Log.e(TAG, "onSuccessAffirmOrder: " + commonResult.toString());
            int status = commonResult.getStatus();
            if (status > 0) {
                switch (orderType) {
                    case 1:
                        tvOrderlistAll.performClick();
                        break;
                    case 4:
                        tvOrderlistTakegoods.performClick();
                        break;

                }
            } else {
                Toast.makeText(OrderListActivity.this, "" + commonResult.getReturnMsg(), Toast.LENGTH_SHORT).show();
            }
        }

        //请求失败
        @Override
        public void onError(String result) {
            Log.e(TAG, "onError: " + result.toString());
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
//        orderListPredenter.getOrderList(AppConstants.COMPANY_ID,code,timestamp,AppConstants.PAGE_SIZE,pageindex,"","","","",AppConstants.FROM_MOBILE);

    }

    /**
     * 初始化数据源
     */
    private void initData() {
        orderListPredenter.onCreate();
        orderListPredenter.attachView(orderListView);
        String safetyCode = MyUtils.getMetaValue(OrderListActivity.this, "safetyCode");
        code = Utils.md5(safetyCode + Utils.getTimeStamp());
        timestamp = Utils.getTimeStamp();
        mList = new ArrayList<>();
        mAdapter = new OrderListLvAdapter(OrderListActivity.this, mList);
        lvOrderlist.setAdapter(mAdapter);
        mAdapter.setChangeOrderStateInterface(this);
        Log.e("aaa", "-ddddddddddddd->" + type);
        Log.e(TAG, "initData: "+code+"=="+timestamp );
        switch (type) {
            case "0":
                tvOrderlistAll.performClick();
                break;
            case "1":
                tvOrderlistNopay.performClick();
                break;
            case "2":
                tvOrderlistDelivergoods.performClick();
                break;
            case "3":
                tvOrderlistTakegoods.performClick();
                break;
            case "4":
                tvOrderlistNogroup.performClick();
                break;
        }
        setRefresh();
    }

    @OnClick({R.id.iv_title_back, R.id.tv_orderlist_all, R.id.tv_orderlist_nopay, R.id.tv_orderlist_nogroup, R.id.tv_orderlist_delivergoods, R.id.tv_orderlist_takegoods})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.iv_title_back:
                onBackPressed();
                break;
            //全部
            case R.id.tv_orderlist_all:
                initColor();
                tvOrderlistAll.setTextColor(getResources().getColor(R.color.ff2828));
                view1.setVisibility(View.VISIBLE);
                orderStatus = "";
                orderType = 1;
                if (mList != null) {
                    mList.clear();
                }
                pageindex=1;
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                lvOrderlist.setAdapter(mAdapter);
                break;
            //待付款
            case R.id.tv_orderlist_nopay:
                orderType = 2;
                initColor();
                tvOrderlistNopay.setTextColor(getResources().getColor(R.color.ff2828));
                view2.setVisibility(View.VISIBLE);
                if (mList != null) {
                    mList.clear();
                }
                orderStatus = "1";
                pageindex=1;
                Log.e(TAG, "onViewClicked: " + "=orderStatus==" + orderStatus);
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                lvOrderlist.setAdapter(mAdapter);
                break;

            //待发货
            case R.id.tv_orderlist_delivergoods:
                orderType = 3;
                initColor();
                tvOrderlistDelivergoods.setTextColor(getResources().getColor(R.color.ff2828));
                view3.setVisibility(View.VISIBLE);
                if (mList != null) {
                    mList.clear();
                }
                orderStatus = "2";
                pageindex=1;
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                lvOrderlist.setAdapter(mAdapter);
                break;
            //待收货
            case R.id.tv_orderlist_takegoods:
                orderType = 4;
                initColor();
                tvOrderlistTakegoods.setTextColor(getResources().getColor(R.color.ff2828));
                view4.setVisibility(View.VISIBLE);
                if (mList != null) {
                    mList.clear();
                }
                orderStatus = "3";
                pageindex=1;
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                lvOrderlist.setAdapter(mAdapter);
                break;
            //待成团
            case R.id.tv_orderlist_nogroup:
                orderType = 5;
                initColor();
                tvOrderlistNogroup.setTextColor(getResources().getColor(R.color.ff2828));
                view5.setVisibility(View.VISIBLE);
                if (mList != null) {
                    mList.clear();
                }
                orderStatus = "4";
                pageindex=1;
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                lvOrderlist.setAdapter(mAdapter);
                break;
        }
    }

    private void initColor() {
        tvOrderlistAll.setTextColor(getResources().getColor(R.color.t333));
        tvOrderlistNopay.setTextColor(getResources().getColor(R.color.t333));
        tvOrderlistNogroup.setTextColor(getResources().getColor(R.color.t333));
        tvOrderlistDelivergoods.setTextColor(getResources().getColor(R.color.t333));
        tvOrderlistTakegoods.setTextColor(getResources().getColor(R.color.t333));
        view1.setVisibility(View.INVISIBLE);
        view2.setVisibility(View.INVISIBLE);
        view3.setVisibility(View.INVISIBLE);
        view4.setVisibility(View.INVISIBLE);
        view5.setVisibility(View.INVISIBLE);
    }

    /**
     * 确认订单
     *
     * @param position
     * @param orderNumber
     */
    @Override
    public void doAffirmOrder(int position, String orderNumber) {
        Log.e(TAG, "doAffirmOrder: " + code + "==timestamp==" + timestamp + "==orderNumber==" + orderNumber);
        orderListPredenter.affirmOrder(AppConstants.COMPANY_ID, code, timestamp, orderNumber);
    }

    /**
     * 取消未支付订单
     *
     * @param position
     * @param orderNumber
     */
    @Override
    public void doCancelOrder(int position, String orderNumber) {
        Log.e(TAG, "doCancelOrder: " + code + "==timestamp==" + timestamp + "==orderNumber==" + orderNumber);

        orderListPredenter.cancelOrder(AppConstants.COMPANY_ID, code, timestamp, orderNumber);
    }

    private void setRefresh() {
        //刷新
        srflOrderlist.setRefreshHeader(new ClassicsHeader(OrderListActivity.this));
        srflOrderlist.setRefreshFooter(new ClassicsFooter(OrderListActivity.this));
//        mRefresh.setEnablePureScrollMode(true);
        srflOrderlist.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                if (mList != null) {
                    mList.clear();
                }
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);

                refreshlayout.finishRefresh(2000/*,true*/);//传入false表示刷新失败
            }
        });
        srflOrderlist.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageindex++;
                orderListPredenter.getOrderList(AppConstants.COMPANY_ID, code, timestamp, AppConstants.PAGE_SIZE, pageindex, orderStatus, "", "", "",type1, AppConstants.FROM_MOBILE);
                refreshlayout.finishLoadMore(2000/*,true*/);//传入false表示加载失败
            }
        });
    }
}
